// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld;

import java.util.ArrayList;
import zombie.Lua.MapObjects;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.characters.SurvivorFactory;
import zombie.core.Rand;
import zombie.core.skinnedmodel.population.Outfit;
import zombie.core.skinnedmodel.visual.HumanVisual;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.types.HandWeapon;
import zombie.iso.BuildingDef;
import zombie.iso.IsoCell;
import zombie.iso.IsoChunk;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.RoomDef;
import zombie.iso.Vector2;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.objects.IsoDeadBody;
import zombie.iso.objects.IsoZombieGiblets;
import zombie.randomizedWorld.randomizedBuilding.RandomizedBuildingBase;
import zombie.util.StringUtils;
import zombie.util.list.PZArrayUtil;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehicleType;
import zombie.vehicles.VehiclesDB2;

public class RandomizedWorldBase {
    private static final Vector2 s_tempVector2 = new Vector2();
    protected int minimumDays = 0;
    protected int maximumDays = 0;
    protected int minimumRooms = 0;
    protected boolean unique = false;
    private boolean rvsVehicleKeyAddedToZombie = false;
    protected String name = null;
    protected String debugLine = "";

    public BaseVehicle addVehicle(IsoMetaGrid.Zone zone, IsoGridSquare sq, IsoChunk chunk, String zoneName, String scriptName, IsoDirections dir) {
        return this.addVehicle(zone, sq, chunk, zoneName, scriptName, null, dir, null);
    }

    public BaseVehicle addVehicleFlipped(
        IsoMetaGrid.Zone zone,
        IsoGridSquare sq,
        IsoChunk chunk,
        String zoneName,
        String scriptName,
        Integer skinIndex,
        IsoDirections dir,
        String specificContainer
    ) {
        if (sq == null) {
            return null;
        } else {
            if (dir == null) {
                dir = IsoDirections.getRandom();
            }

            Vector2 vector2 = dir.ToVector();
            return this.addVehicleFlipped(
                zone, (float)sq.x, (float)sq.y, (float)sq.z, vector2.getDirection(), zoneName, scriptName, skinIndex, specificContainer
            );
        }
    }

    public BaseVehicle addVehicleFlipped(
        IsoMetaGrid.Zone zone,
        float vehicleX,
        float vehicleY,
        float vehicleZ,
        float direction,
        String zoneName,
        String scriptName,
        Integer skinIndex,
        String specificContainer
    ) {
        if (StringUtils.isNullOrEmpty(zoneName)) {
            zoneName = "junkyard";
        }

        IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare((double)vehicleX, (double)vehicleY, (double)vehicleZ);
        if (isoGridSquare == null) {
            return null;
        } else {
            IsoChunk isoChunk = isoGridSquare.getChunk();
            IsoDirections isoDirections = IsoDirections.fromAngle(direction);
            BaseVehicle baseVehicle = new BaseVehicle(IsoWorld.instance.CurrentCell);
            baseVehicle.specificDistributionId = specificContainer;
            VehicleType vehicleType = VehicleType.getRandomVehicleType(zoneName, false);
            if (!StringUtils.isNullOrEmpty(scriptName)) {
                baseVehicle.setScriptName(scriptName);
                baseVehicle.setScript();
                if (skinIndex != null) {
                    baseVehicle.setSkinIndex(skinIndex);
                }
            } else {
                if (vehicleType == null) {
                    return null;
                }

                baseVehicle.setVehicleType(vehicleType.name);
                if (!isoChunk.RandomizeModel(baseVehicle, zone, zoneName, vehicleType)) {
                    return null;
                }
            }

            if (vehicleType.isSpecialCar) {
                baseVehicle.setDoColor(false);
            }

            baseVehicle.setDir(isoDirections);
            float _float = direction - (float) (Math.PI / 2);

            while ((double)_float > Math.PI * 2) {
                _float = (float)((double)_float - (Math.PI * 2));
            }

            baseVehicle.savedRot.rotationXYZ(0.0F, -_float, (float) Math.PI);
            baseVehicle.jniTransform.setRotation(baseVehicle.savedRot);
            baseVehicle.setX(vehicleX);
            baseVehicle.setY(vehicleY);
            baseVehicle.setZ(vehicleZ);
            if (IsoChunk.doSpawnedVehiclesInInvalidPosition(baseVehicle)) {
                baseVehicle.setSquare(isoGridSquare);
                isoGridSquare.chunk.vehicles.add(baseVehicle);
                baseVehicle.chunk = isoGridSquare.chunk;
                baseVehicle.addToWorld();
                VehiclesDB2.instance.addVehicle(baseVehicle);
            }

            baseVehicle.setGeneralPartCondition(0.2F, 70.0F);
            baseVehicle.rust = Rand.Next(100) < 70 ? 1.0F : 0.0F;
            return baseVehicle;
        }
    }

    public BaseVehicle addVehicle(
        IsoMetaGrid.Zone zone,
        IsoGridSquare sq,
        IsoChunk chunk,
        String zoneName,
        String scriptName,
        Integer skinIndex,
        IsoDirections dir,
        String specificContainer
    ) {
        if (sq == null) {
            return null;
        } else {
            if (dir == null) {
                dir = IsoDirections.getRandom();
            }

            Vector2 vector2 = dir.ToVector();
            vector2.rotate(Rand.Next(-0.5F, 0.5F));
            return this.addVehicle(zone, (float)sq.x, (float)sq.y, (float)sq.z, vector2.getDirection(), zoneName, scriptName, skinIndex, specificContainer);
        }
    }

    public BaseVehicle addVehicle(
        IsoMetaGrid.Zone zone,
        float vehicleX,
        float vehicleY,
        float vehicleZ,
        float direction,
        String zoneName,
        String scriptName,
        Integer skinIndex,
        String specificContainer
    ) {
        if (StringUtils.isNullOrEmpty(zoneName)) {
            zoneName = "junkyard";
        }

        IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare((double)vehicleX, (double)vehicleY, (double)vehicleZ);
        if (isoGridSquare == null) {
            return null;
        } else {
            IsoChunk isoChunk = isoGridSquare.getChunk();
            IsoDirections isoDirections = IsoDirections.fromAngle(direction);
            BaseVehicle baseVehicle = new BaseVehicle(IsoWorld.instance.CurrentCell);
            baseVehicle.specificDistributionId = specificContainer;
            VehicleType vehicleType = VehicleType.getRandomVehicleType(zoneName, false);
            if (!StringUtils.isNullOrEmpty(scriptName)) {
                baseVehicle.setScriptName(scriptName);
                baseVehicle.setScript();
                if (skinIndex != null) {
                    baseVehicle.setSkinIndex(skinIndex);
                }
            } else {
                if (vehicleType == null) {
                    return null;
                }

                baseVehicle.setVehicleType(vehicleType.name);
                if (!isoChunk.RandomizeModel(baseVehicle, zone, zoneName, vehicleType)) {
                    return null;
                }
            }

            if (vehicleType.isSpecialCar) {
                baseVehicle.setDoColor(false);
            }

            baseVehicle.setDir(isoDirections);
            float _float = direction - (float) (Math.PI / 2);

            while ((double)_float > Math.PI * 2) {
                _float = (float)((double)_float - (Math.PI * 2));
            }

            baseVehicle.savedRot.setAngleAxis(-_float, 0.0F, 1.0F, 0.0F);
            baseVehicle.jniTransform.setRotation(baseVehicle.savedRot);
            baseVehicle.setX(vehicleX);
            baseVehicle.setY(vehicleY);
            baseVehicle.setZ(vehicleZ);
            if (IsoChunk.doSpawnedVehiclesInInvalidPosition(baseVehicle)) {
                baseVehicle.setSquare(isoGridSquare);
                isoGridSquare.chunk.vehicles.add(baseVehicle);
                baseVehicle.chunk = isoGridSquare.chunk;
                baseVehicle.addToWorld();
                VehiclesDB2.instance.addVehicle(baseVehicle);
            }

            baseVehicle.setGeneralPartCondition(0.2F, 70.0F);
            baseVehicle.rust = Rand.Next(100) < 70 ? 1.0F : 0.0F;
            return baseVehicle;
        }
    }

    public static void removeAllVehiclesOnZone(IsoMetaGrid.Zone zone) {
        for (int int0 = zone.x; int0 < zone.x + zone.w; int0++) {
            for (int int1 = zone.y; int1 < zone.y + zone.h; int1++) {
                IsoGridSquare isoGridSquare = IsoCell.getInstance().getGridSquare(int0, int1, 0);
                if (isoGridSquare != null) {
                    BaseVehicle baseVehicle = isoGridSquare.getVehicleContainer();
                    if (baseVehicle != null) {
                        baseVehicle.permanentlyRemove();
                    }
                }
            }
        }
    }

    public ArrayList<IsoZombie> addZombiesOnVehicle(int totalZombies, String outfit, Integer femaleChance, BaseVehicle vehicle) {
        ArrayList arrayList = new ArrayList();
        if (vehicle == null) {
            return arrayList;
        } else {
            int _int = 100;
            IsoGridSquare isoGridSquare0 = vehicle.getSquare();
            if (isoGridSquare0 != null && isoGridSquare0.getCell() != null) {
                for (; totalZombies > 0; _int = 100) {
                    while (_int > 0) {
                        IsoGridSquare isoGridSquare1 = isoGridSquare0.getCell()
                            .getGridSquare(
                                Rand.Next(isoGridSquare0.x - 4, isoGridSquare0.x + 4), Rand.Next(isoGridSquare0.y - 4, isoGridSquare0.y + 4), isoGridSquare0.z
                            );
                        if (isoGridSquare1 != null && isoGridSquare1.getVehicleContainer() == null) {
                            totalZombies--;
                            arrayList.addAll(this.addZombiesOnSquare(1, outfit, femaleChance, isoGridSquare1));
                            break;
                        }

                        _int--;
                    }
                }

                if (!this.rvsVehicleKeyAddedToZombie && !arrayList.isEmpty()) {
                    IsoZombie isoZombie = (IsoZombie)arrayList.get(Rand.Next(0, arrayList.size()));
                    isoZombie.addItemToSpawnAtDeath(vehicle.createVehicleKey());
                    this.rvsVehicleKeyAddedToZombie = true;
                }

                return arrayList;
            } else {
                return arrayList;
            }
        }
    }

    public static IsoDeadBody createRandomDeadBody(RoomDef room, int blood) {
        if (IsoWorld.getZombiesDisabled()) {
            return null;
        } else if (room == null) {
            return null;
        } else {
            IsoGridSquare isoGridSquare = getRandomSquareForCorpse(room);
            return isoGridSquare == null ? null : createRandomDeadBody(isoGridSquare, null, blood, 0, null);
        }
    }

    public ArrayList<IsoZombie> addZombiesOnSquare(int totalZombies, String outfit, Integer femaleChance, IsoGridSquare square) {
        ArrayList arrayList = new ArrayList();
        if (IsoWorld.getZombiesDisabled()) {
            return arrayList;
        } else if (square == null) {
            return arrayList;
        } else {
            for (int _int = 0; _int < totalZombies; _int++) {
                zombie.VirtualZombieManager.instance.choices.clear();
                zombie.VirtualZombieManager.instance.choices.add(square);
                IsoZombie isoZombie = zombie.VirtualZombieManager.instance.createRealZombieAlways(IsoDirections.getRandom().index(), false);
                if (isoZombie != null) {
                    if (femaleChance != null) {
                        isoZombie.setFemaleEtc(Rand.Next(100) < femaleChance);
                    }

                    if (outfit != null) {
                        isoZombie.dressInPersistentOutfit(outfit);
                        isoZombie.bDressInRandomOutfit = false;
                    } else {
                        isoZombie.dressInRandomOutfit();
                        isoZombie.bDressInRandomOutfit = false;
                    }

                    arrayList.add(isoZombie);
                }
            }

            zombie.ZombieSpawnRecorder.instance.record(arrayList, this.getClass().getSimpleName());
            return arrayList;
        }
    }

    public static IsoDeadBody createRandomDeadBody(int x, int y, int z, IsoDirections dir, int blood) {
        return createRandomDeadBody(x, y, z, dir, blood, 0);
    }

    public static IsoDeadBody createRandomDeadBody(int x, int y, int z, IsoDirections dir, int blood, int crawlerChance) {
        IsoGridSquare isoGridSquare = IsoCell.getInstance().getGridSquare(x, y, z);
        return createRandomDeadBody(isoGridSquare, dir, blood, crawlerChance, null);
    }

    public static IsoDeadBody createRandomDeadBody(IsoGridSquare sq, IsoDirections dir, int blood, int crawlerChance, String outfit) {
        if (sq == null) {
            return null;
        } else {
            boolean _boolean = dir == null;
            if (_boolean) {
                dir = IsoDirections.getRandom();
            }

            return createRandomDeadBody(
                (float)sq.x + Rand.Next(0.05F, 0.95F),
                (float)sq.y + Rand.Next(0.05F, 0.95F),
                (float)sq.z,
                dir.ToVector().getDirection(),
                _boolean,
                blood,
                crawlerChance,
                outfit
            );
        }
    }

    public static IsoDeadBody createRandomDeadBody(
        float x, float y, float z, float direction, boolean alignToSquare, int blood, int crawlerChance, String outfit
    ) {
        if (IsoWorld.getZombiesDisabled()) {
            return null;
        } else {
            IsoGridSquare isoGridSquare = IsoCell.getInstance().getGridSquare((double)x, (double)y, (double)z);
            if (isoGridSquare == null) {
                return null;
            } else {
                IsoDirections isoDirections = IsoDirections.fromAngle(direction);
                zombie.VirtualZombieManager.instance.choices.clear();
                zombie.VirtualZombieManager.instance.choices.add(isoGridSquare);
                IsoZombie isoZombie = zombie.VirtualZombieManager.instance.createRealZombieAlways(isoDirections.index(), false);
                if (isoZombie == null) {
                    return null;
                } else {
                    if (outfit != null) {
                        isoZombie.dressInPersistentOutfit(outfit);
                        isoZombie.bDressInRandomOutfit = false;
                    } else {
                        isoZombie.dressInRandomOutfit();
                    }

                    if (Rand.Next(100) < crawlerChance) {
                        isoZombie.setFakeDead(true);
                        isoZombie.setCrawler(true);
                        isoZombie.setCanWalk(false);
                        isoZombie.setCrawlerType(1);
                    } else {
                        isoZombie.setFakeDead(false);
                        isoZombie.setHealth(0.0F);
                    }

                    isoZombie.upKillCount = false;
                    isoZombie.getHumanVisual().zombieRotStage = ((HumanVisual)isoZombie.getVisual()).pickRandomZombieRotStage();

                    for (int _int = 0; _int < blood; _int++) {
                        isoZombie.addBlood(null, false, true, true);
                    }

                    isoZombie.DoCorpseInventory();
                    isoZombie.setX(x);
                    isoZombie.setY(y);
                    isoZombie.getForwardDirection().setLengthAndDirection(direction, 1.0F);
                    if (alignToSquare || isoZombie.isSkeleton()) {
                        alignCorpseToSquare(isoZombie, isoGridSquare);
                    }

                    IsoDeadBody isoDeadBody = new IsoDeadBody(isoZombie, true);
                    if (!isoDeadBody.isFakeDead() && !isoDeadBody.isSkeleton() && Rand.Next(20) == 0) {
                        isoDeadBody.setFakeDead(true);
                        if (Rand.Next(5) == 0) {
                            isoDeadBody.setCrawling(true);
                        }
                    }

                    return isoDeadBody;
                }
            }
        }
    }

    public void addTraitOfBlood(IsoDirections dir, int time, int x, int y, int z) {
        for (int _int = 0; _int < time; _int++) {
            float float0 = 0.0F;
            float float1 = 0.0F;
            if (dir == IsoDirections.S) {
                float1 = Rand.Next(-2.0F, 0.5F);
            }

            if (dir == IsoDirections.N) {
                float1 = Rand.Next(-0.5F, 2.0F);
            }

            if (dir == IsoDirections.E) {
                float0 = Rand.Next(-2.0F, 0.5F);
            }

            if (dir == IsoDirections.W) {
                float0 = Rand.Next(-0.5F, 2.0F);
            }

            new IsoZombieGiblets(IsoZombieGiblets.GibletType.A, IsoCell.getInstance(), (float)x, (float)y, (float)z + 0.2F, float0, float1);
        }
    }

    public void addTrailOfBlood(float x, float y, float z, float direction, int count) {
        Vector2 vector2 = s_tempVector2;

        for (int _int = 0; _int < count; _int++) {
            float _float = Rand.Next(-0.5F, 2.0F);
            if (_float < 0.0F) {
                vector2.setLengthAndDirection(direction + (float) Math.PI, -_float);
            } else {
                vector2.setLengthAndDirection(direction, _float);
            }

            new IsoZombieGiblets(IsoZombieGiblets.GibletType.A, IsoCell.getInstance(), x, y, z + 0.2F, vector2.x, vector2.y);
        }
    }

    public void addBloodSplat(IsoGridSquare sq, int nbr) {
        for (int _int = 0; _int < nbr; _int++) {
            sq.getChunk().addBloodSplat((float)sq.x + Rand.Next(-0.5F, 0.5F), (float)sq.y + Rand.Next(-0.5F, 0.5F), (float)sq.z, Rand.Next(8));
        }
    }

    public void setAttachedItem(IsoZombie zombie, String location, String item, String ensureItem) {
        InventoryItem inventoryItem = InventoryItemFactory.CreateItem(item);
        if (inventoryItem != null) {
            inventoryItem.setCondition(Rand.Next(Math.max(2, inventoryItem.getConditionMax() - 5), inventoryItem.getConditionMax()));
            if (inventoryItem instanceof HandWeapon) {
                ((HandWeapon)inventoryItem).randomizeBullets();
            }

            zombie.setAttachedItem(location, inventoryItem);
            if (!StringUtils.isNullOrEmpty(ensureItem)) {
                zombie.addItemToSpawnAtDeath(InventoryItemFactory.CreateItem(ensureItem));
            }
        }
    }

    public static IsoGameCharacter createRandomZombie(RoomDef room) {
        IsoGridSquare isoGridSquare = getRandomSpawnSquare(room);
        return createRandomZombie(isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ());
    }

    public static IsoGameCharacter createRandomZombieForCorpse(RoomDef room) {
        IsoGridSquare isoGridSquare = getRandomSquareForCorpse(room);
        if (isoGridSquare == null) {
            return null;
        } else {
            IsoGameCharacter isoGameCharacter = createRandomZombie(isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ());
            if (isoGameCharacter != null) {
                alignCorpseToSquare(isoGameCharacter, isoGridSquare);
            }

            return isoGameCharacter;
        }
    }

    public static IsoDeadBody createBodyFromZombie(IsoGameCharacter chr) {
        if (IsoWorld.getZombiesDisabled()) {
            return null;
        } else {
            for (int _int = 0; _int < 6; _int++) {
                chr.splatBlood(Rand.Next(1, 4), 0.3F);
            }

            return new IsoDeadBody(chr, true);
        }
    }

    public static IsoGameCharacter createRandomZombie(int x, int y, int z) {
        RandomizedBuildingBase.HumanCorpse humanCorpse = new RandomizedBuildingBase.HumanCorpse(IsoWorld.instance.getCell(), (float)x, (float)y, (float)z);
        humanCorpse.setDescriptor(SurvivorFactory.CreateSurvivor());
        humanCorpse.setFemale(humanCorpse.getDescriptor().isFemale());
        humanCorpse.setDir(IsoDirections.fromIndex(Rand.Next(8)));
        humanCorpse.initWornItems("Human");
        humanCorpse.initAttachedItems("Human");
        Outfit outfit = humanCorpse.getRandomDefaultOutfit();
        humanCorpse.dressInNamedOutfit(outfit.m_Name);
        humanCorpse.initSpritePartsEmpty();
        humanCorpse.Dressup(humanCorpse.getDescriptor());
        return humanCorpse;
    }

    private static boolean isSquareClear(IsoGridSquare isoGridSquare) {
        return isoGridSquare != null
            && canSpawnAt(isoGridSquare)
            && !isoGridSquare.HasStairs()
            && !isoGridSquare.HasTree()
            && !isoGridSquare.getProperties().Is(IsoFlagType.bed)
            && !isoGridSquare.getProperties().Is(IsoFlagType.waterPiped);
    }

    private static boolean isSquareClear(IsoGridSquare isoGridSquare1, IsoDirections isoDirections) {
        IsoGridSquare isoGridSquare0 = isoGridSquare1.getAdjacentSquare(isoDirections);
        return isSquareClear(isoGridSquare0) && !isoGridSquare1.isSomethingTo(isoGridSquare0) && isoGridSquare1.getRoomID() == isoGridSquare0.getRoomID();
    }

    public static boolean is1x2AreaClear(IsoGridSquare square) {
        return isSquareClear(square) && isSquareClear(square, IsoDirections.N);
    }

    public static boolean is2x1AreaClear(IsoGridSquare square) {
        return isSquareClear(square) && isSquareClear(square, IsoDirections.W);
    }

    public static boolean is2x1or1x2AreaClear(IsoGridSquare square) {
        return isSquareClear(square) && (isSquareClear(square, IsoDirections.W) || isSquareClear(square, IsoDirections.N));
    }

    public static boolean is2x2AreaClear(IsoGridSquare square) {
        return isSquareClear(square)
            && isSquareClear(square, IsoDirections.N)
            && isSquareClear(square, IsoDirections.W)
            && isSquareClear(square, IsoDirections.NW);
    }

    public static void alignCorpseToSquare(IsoGameCharacter chr, IsoGridSquare square) {
        int int0 = square.x;
        int int1 = square.y;
        IsoDirections isoDirections = IsoDirections.fromIndex(Rand.Next(8));
        boolean boolean0 = is1x2AreaClear(square);
        boolean boolean1 = is2x1AreaClear(square);
        if (boolean0 && boolean1) {
            boolean0 = Rand.Next(2) == 0;
            boolean1 = !boolean0;
        }

        if (is2x2AreaClear(square)) {
            chr.setX((float)int0);
            chr.setY((float)int1);
        } else if (boolean0) {
            chr.setX((float)int0 + 0.5F);
            chr.setY((float)int1);
            isoDirections = Rand.Next(2) == 0 ? IsoDirections.N : IsoDirections.S;
        } else if (boolean1) {
            chr.setX((float)int0);
            chr.setY((float)int1 + 0.5F);
            isoDirections = Rand.Next(2) == 0 ? IsoDirections.W : IsoDirections.E;
        } else if (is1x2AreaClear(square.getAdjacentSquare(IsoDirections.S))) {
            chr.setX((float)int0 + 0.5F);
            chr.setY((float)int1 + 0.99F);
            isoDirections = Rand.Next(2) == 0 ? IsoDirections.N : IsoDirections.S;
        } else if (is2x1AreaClear(square.getAdjacentSquare(IsoDirections.E))) {
            chr.setX((float)int0 + 0.99F);
            chr.setY((float)int1 + 0.5F);
            isoDirections = Rand.Next(2) == 0 ? IsoDirections.W : IsoDirections.E;
        }

        chr.setDir(isoDirections);
        chr.lx = chr.nx = chr.x;
        chr.ly = chr.ny = chr.y;
        chr.setScriptnx(chr.x);
        chr.setScriptny(chr.y);
    }

    public RoomDef getRandomRoom(BuildingDef bDef, int minArea) {
        RoomDef roomDef = (RoomDef)bDef.getRooms().get(Rand.Next(0, bDef.getRooms().size()));
        if (minArea > 0 && roomDef.area >= minArea) {
            return roomDef;
        } else {
            int _int = 0;

            while (_int <= 20) {
                _int++;
                roomDef = (RoomDef)bDef.getRooms().get(Rand.Next(0, bDef.getRooms().size()));
                if (roomDef.area >= minArea) {
                    return roomDef;
                }
            }

            return roomDef;
        }
    }

    public RoomDef getRoom(BuildingDef bDef, String roomName) {
        for (int _int = 0; _int < bDef.rooms.size(); _int++) {
            RoomDef roomDef = (RoomDef)bDef.rooms.get(_int);
            if (roomDef.getName().equalsIgnoreCase(roomName)) {
                return roomDef;
            }
        }

        return null;
    }

    public RoomDef getLivingRoomOrKitchen(BuildingDef bDef) {
        RoomDef roomDef = this.getRoom(bDef, "livingroom");
        if (roomDef == null) {
            roomDef = this.getRoom(bDef, "kitchen");
        }

        return roomDef;
    }

    private static boolean canSpawnAt(IsoGridSquare isoGridSquare) {
        if (isoGridSquare == null) {
            return false;
        } else {
            return isoGridSquare.HasStairs() ? false : zombie.VirtualZombieManager.instance.canSpawnAt(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z);
        }
    }

    public static IsoGridSquare getRandomSpawnSquare(RoomDef roomDef) {
        return roomDef == null ? null : roomDef.getRandomSquare(RandomizedWorldBase::canSpawnAt);
    }

    public static IsoGridSquare getRandomSquareForCorpse(RoomDef roomDef) {
        IsoGridSquare isoGridSquare0 = roomDef.getRandomSquare(RandomizedWorldBase::is2x2AreaClear);
        IsoGridSquare isoGridSquare1 = roomDef.getRandomSquare(RandomizedWorldBase::is2x1or1x2AreaClear);
        if (isoGridSquare0 == null || isoGridSquare1 != null && Rand.Next(4) == 0) {
            isoGridSquare0 = isoGridSquare1;
        }

        return isoGridSquare0;
    }

    public BaseVehicle spawnCarOnNearestNav(String carName, BuildingDef def) {
        IsoGridSquare isoGridSquare0 = null;
        int int0 = (def.x + def.x2) / 2;
        int int1 = (def.y + def.y2) / 2;

        for (int int2 = int0; int2 < int0 + 20; int2++) {
            IsoGridSquare isoGridSquare1 = IsoCell.getInstance().getGridSquare(int2, int1, 0);
            if (isoGridSquare1 != null && "Nav".equals(isoGridSquare1.getZoneType())) {
                isoGridSquare0 = isoGridSquare1;
                break;
            }
        }

        if (isoGridSquare0 != null) {
            return this.spawnCar(carName, isoGridSquare0);
        } else {
            for (int int3 = int0; int3 > int0 - 20; int3--) {
                IsoGridSquare isoGridSquare2 = IsoCell.getInstance().getGridSquare(int3, int1, 0);
                if (isoGridSquare2 != null && "Nav".equals(isoGridSquare2.getZoneType())) {
                    isoGridSquare0 = isoGridSquare2;
                    break;
                }
            }

            if (isoGridSquare0 != null) {
                return this.spawnCar(carName, isoGridSquare0);
            } else {
                for (int int4 = int1; int4 < int1 + 20; int4++) {
                    IsoGridSquare isoGridSquare3 = IsoCell.getInstance().getGridSquare(int0, int4, 0);
                    if (isoGridSquare3 != null && "Nav".equals(isoGridSquare3.getZoneType())) {
                        isoGridSquare0 = isoGridSquare3;
                        break;
                    }
                }

                if (isoGridSquare0 != null) {
                    return this.spawnCar(carName, isoGridSquare0);
                } else {
                    for (int int5 = int1; int5 > int1 - 20; int5--) {
                        IsoGridSquare isoGridSquare4 = IsoCell.getInstance().getGridSquare(int0, int5, 0);
                        if (isoGridSquare4 != null && "Nav".equals(isoGridSquare4.getZoneType())) {
                            isoGridSquare0 = isoGridSquare4;
                            break;
                        }
                    }

                    return isoGridSquare0 != null ? this.spawnCar(carName, isoGridSquare0) : null;
                }
            }
        }
    }

    private BaseVehicle spawnCar(String string, IsoGridSquare isoGridSquare) {
        BaseVehicle baseVehicle = new BaseVehicle(IsoWorld.instance.CurrentCell);
        baseVehicle.setScriptName(string);
        baseVehicle.setX((float)isoGridSquare.x + 0.5F);
        baseVehicle.setY((float)isoGridSquare.y + 0.5F);
        baseVehicle.setZ(0.0F);
        baseVehicle.savedRot.setAngleAxis(Rand.Next(0.0F, (float) (Math.PI * 2)), 0.0F, 1.0F, 0.0F);
        baseVehicle.jniTransform.setRotation(baseVehicle.savedRot);
        if (IsoChunk.doSpawnedVehiclesInInvalidPosition(baseVehicle)) {
            baseVehicle.keySpawned = 1;
            baseVehicle.setSquare(isoGridSquare);
            baseVehicle.square.chunk.vehicles.add(baseVehicle);
            baseVehicle.chunk = baseVehicle.square.chunk;
            baseVehicle.addToWorld();
            VehiclesDB2.instance.addVehicle(baseVehicle);
        }

        baseVehicle.setGeneralPartCondition(0.3F, 70.0F);
        return baseVehicle;
    }

    public InventoryItem addItemOnGround(IsoGridSquare square, String type) {
        return square != null && !StringUtils.isNullOrWhitespace(type)
            ? square.AddWorldInventoryItem(type, Rand.Next(0.2F, 0.8F), Rand.Next(0.2F, 0.8F), 0.0F)
            : null;
    }

    public InventoryItem addItemOnGround(IsoGridSquare square, InventoryItem item) {
        return square != null && item != null ? square.AddWorldInventoryItem(item, Rand.Next(0.2F, 0.8F), Rand.Next(0.2F, 0.8F), 0.0F) : null;
    }

    public void addRandomItemsOnGround(RoomDef room, String type, int count) {
        for (int _int = 0; _int < count; _int++) {
            IsoGridSquare isoGridSquare = getRandomSpawnSquare(room);
            this.addItemOnGround(isoGridSquare, type);
        }
    }

    public void addRandomItemsOnGround(RoomDef room, ArrayList<String> types, int count) {
        for (int _int = 0; _int < count; _int++) {
            IsoGridSquare isoGridSquare = getRandomSpawnSquare(room);
            this.addRandomItemOnGround(isoGridSquare, types);
        }
    }

    public InventoryItem addRandomItemOnGround(IsoGridSquare square, ArrayList<String> types) {
        if (square != null && !types.isEmpty()) {
            String string = PZArrayUtil.pickRandom(types);
            return this.addItemOnGround(square, string);
        } else {
            return null;
        }
    }

    public HandWeapon addWeapon(String type, boolean addRandomBullets) {
        HandWeapon handWeapon = (HandWeapon)InventoryItemFactory.CreateItem(type);
        if (handWeapon == null) {
            return null;
        } else {
            if (handWeapon.isRanged() && addRandomBullets) {
                if (!StringUtils.isNullOrWhitespace(handWeapon.getMagazineType())) {
                    handWeapon.setContainsClip(true);
                }

                handWeapon.setCurrentAmmoCount(Rand.Next(Math.max(handWeapon.getMaxAmmo() - 8, 0), handWeapon.getMaxAmmo() - 2));
            }

            return handWeapon;
        }
    }

    public IsoDeadBody createSkeletonCorpse(RoomDef room) {
        if (room == null) {
            return null;
        } else {
            IsoGridSquare isoGridSquare = room.getRandomSquare(RandomizedWorldBase::is2x1or1x2AreaClear);
            if (isoGridSquare == null) {
                return null;
            } else {
                zombie.VirtualZombieManager.instance.choices.clear();
                zombie.VirtualZombieManager.instance.choices.add(isoGridSquare);
                IsoZombie isoZombie = zombie.VirtualZombieManager.instance.createRealZombieAlways(Rand.Next(8), false);
                if (isoZombie == null) {
                    return null;
                } else {
                    zombie.ZombieSpawnRecorder.instance.record(isoZombie, this.getClass().getSimpleName());
                    alignCorpseToSquare(isoZombie, isoGridSquare);
                    isoZombie.setFakeDead(false);
                    isoZombie.setHealth(0.0F);
                    isoZombie.upKillCount = false;
                    isoZombie.setSkeleton(true);
                    isoZombie.getHumanVisual().setSkinTextureIndex(Rand.Next(1, 3));
                    return new IsoDeadBody(isoZombie, true);
                }
            }
        }
    }

    public boolean isTimeValid(boolean force) {
        if (this.minimumDays != 0 && this.maximumDays != 0) {
            float _float = (float)zombie.GameTime.getInstance().getWorldAgeHours() / 24.0F;
            _float += (float)((zombie.SandboxOptions.instance.TimeSinceApo.getValue() - 1) * 30);
            return this.minimumDays > 0 && _float < (float)this.minimumDays ? false : this.maximumDays <= 0 || !(_float > (float)this.maximumDays);
        } else {
            return true;
        }
    }

    public String getName() {
        return this.name;
    }

    public String getDebugLine() {
        return this.debugLine;
    }

    public void setDebugLine(String _debugLine) {
        this.debugLine = _debugLine;
    }

    public int getMaximumDays() {
        return this.maximumDays;
    }

    public void setMaximumDays(int _maximumDays) {
        this.maximumDays = _maximumDays;
    }

    public boolean isUnique() {
        return this.unique;
    }

    public void setUnique(boolean _unique) {
        this.unique = _unique;
    }

    public IsoGridSquare getSq(int x, int y, int z) {
        return IsoWorld.instance.getCell().getGridSquare(x, y, z);
    }

    public IsoObject addTileObject(int x, int y, int z, String spriteName) {
        return this.addTileObject(this.getSq(x, y, z), spriteName);
    }

    public IsoObject addTileObject(IsoGridSquare sq, String spriteName) {
        if (sq == null) {
            return null;
        } else {
            IsoObject isoObject = IsoObject.getNew(sq, spriteName, null, false);
            sq.AddTileObject(isoObject);
            MapObjects.newGridSquare(sq);
            MapObjects.loadGridSquare(sq);
            return isoObject;
        }
    }

    public IsoObject addTentNorthSouth(int x, int y, int z) {
        this.addTileObject(x, y - 1, z, "camping_01_1");
        return this.addTileObject(x, y, z, "camping_01_0");
    }

    public IsoObject addTentWestEast(int x, int y, int z) {
        this.addTileObject(x - 1, y, z, "camping_01_2");
        return this.addTileObject(x, y, z, "camping_01_3");
    }

    public BaseVehicle addTrailer(BaseVehicle v, IsoMetaGrid.Zone zone, IsoChunk chunk, String zoneName, String vehicleDistrib, String trailerName) {
        IsoGridSquare isoGridSquare = v.getSquare();
        IsoDirections isoDirections = v.getDir();
        byte byte0 = 0;
        byte byte1 = 0;
        if (isoDirections == IsoDirections.S) {
            byte1 = -3;
        }

        if (isoDirections == IsoDirections.N) {
            byte1 = 3;
        }

        if (isoDirections == IsoDirections.W) {
            byte0 = 3;
        }

        if (isoDirections == IsoDirections.E) {
            byte0 = -3;
        }

        BaseVehicle baseVehicle = this.addVehicle(
            zone,
            this.getSq(isoGridSquare.x + byte0, isoGridSquare.y + byte1, isoGridSquare.z),
            chunk,
            zoneName,
            trailerName,
            null,
            isoDirections,
            vehicleDistrib
        );
        if (baseVehicle != null) {
            v.positionTrailer(baseVehicle);
        }

        return baseVehicle;
    }
}
